package com.taotao.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.apache.activemq.command.ActiveMQTopic;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.pojo.EasyUIDataGridResult;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.util.IDUtils;
import com.taotao.common.util.JsonUtils;
import com.taotao.mapper.TbItemDescMapper;
import com.taotao.mapper.TbItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemDesc;
import com.taotao.pojo.TbItemExample;
import com.taotao.service.ItemService;
import com.taotao.service.JedisClient;

@Service
public class ItemServiceImpl implements ItemService{

	//1.注入mapper接口
	@Autowired
	private TbItemMapper mapper;
	
	@Autowired
	private TbItemDescMapper descmapper;
	
	//注入JmsTemplate
	@Autowired
	private JmsTemplate jmsTemplate;
	
	//注入topic
	@Resource(name="topicDestination")
	private ActiveMQTopic topicDestination;
	
	//注入Jeids客户端
	@Autowired
	private JedisClient jedisClient;
	
	//商品基本信息
	@Value("${ITEM_INFO_KEY}")
	private String ITEM_INFO_KEY;
	
	//商品过期时间
	@Value("${ITEM_INFO_KEY_EXPIRE}")
	private Integer ITEM_INFO_KEY_EXPIRE;

	
	@Override
	public EasyUIDataGridResult getItemList(Integer nowPage, Integer rows) {
		
		//2.设置分页信息
		if(null == nowPage) nowPage = 1;
		if(null == rows) rows = 30;
		PageHelper.startPage(nowPage, rows);
		//3.调用mapper的方法
		TbItemExample example = new TbItemExample();
		List<TbItem> list = mapper.selectByExample(example);
		//4.获得分页信息
		PageInfo<TbItem> info = new PageInfo<TbItem>(list);
		//5.封装到EasyUIDataGridResult
		EasyUIDataGridResult result = new EasyUIDataGridResult();
		result.setRows(info.getList());
		result.setTotal((int)info.getTotal());
		return result;
	}
	
	
	@Override
	public TaotaoResult saveItem(TbItem item, String desc) {
		//生成商品的id
		long itemId = IDUtils.genItemId();
		//1.补全item 的其他属性
		item.setId(itemId);
		item.setCreated(new Date());
		//1-正常，2-下架，3-删除',
		item.setStatus((byte) 1);
		item.setUpdated(item.getCreated());
		//2.插入到item表 商品的基本信息表
		mapper.insertSelective(item);
		//3.补全商品描述中的属性
		TbItemDesc desc2 = new TbItemDesc();
		desc2.setItemDesc(desc);
		desc2.setItemId(itemId);
		desc2.setCreated(item.getCreated());
		desc2.setUpdated(item.getCreated());
		//4.插入商品描述数据
			//注入tbitemdesc的mapper
		descmapper.insertSelective(desc2);
		
		//5.返回taotaoresult
		return TaotaoResult.ok(item);
	}

	@Override
	public TaotaoResult UpdateSolrFiled(TbItem tbItem, String desc) {
		TaotaoResult saveStatus = saveItem(tbItem,desc);
		
		if(saveStatus.getStatus() == 200) {
			//通过activeMq发送消息
			jmsTemplate.send(topicDestination,new MessageCreator() {

				@Override
				public Message createMessage(Session session) throws JMSException {
					
					return session.createTextMessage(tbItem.getId()+"");
				}
			});
			
		}
		return TaotaoResult.ok();
	}


	@Override
	public TbItem getItemById(long itemId) {
		
		try {
			//判断是否有缓存，有的话直接返回，并重置过期时间
			String itemStr = jedisClient.get(ITEM_INFO_KEY + ":" + itemId + ":BASE");
			if (StringUtils.isNotEmpty(itemStr)) {
				System.out.println("已有缓存");
				jedisClient.expire(ITEM_INFO_KEY + ":" + itemId + ":BASE",ITEM_INFO_KEY_EXPIRE);
				return JsonUtils.jsonToPojo(itemStr, TbItem.class);
			} 
		} catch (Exception e) {
			e.printStackTrace();
		}
		TbItem tbItem = mapper.selectByPrimaryKey(itemId);
		
	    //如果没缓存添加缓存，并设置过期时间
	    try {
			jedisClient.set(ITEM_INFO_KEY + ":" + itemId + ":BASE", JsonUtils.objectToJson(tbItem));
			jedisClient.expire(ITEM_INFO_KEY + ":" + itemId + ":BASE", ITEM_INFO_KEY_EXPIRE);
		} catch (Exception e) {
			e.printStackTrace();
		}
	    
		return tbItem;
	}


	@Override
	public TbItemDesc getDescById(long itemId) {
		
		try {
			//判断是否有缓存，有的话直接返回，并重置过期时间
			String itemStr = jedisClient.get(ITEM_INFO_KEY + ":" + itemId + ":DESC");
			if (StringUtils.isNotEmpty(itemStr)) {
				System.out.println("已有缓存");
				jedisClient.expire(ITEM_INFO_KEY + ":" + itemId + ":DESC",ITEM_INFO_KEY_EXPIRE);
				return JsonUtils.jsonToPojo(itemStr, TbItemDesc.class);
			} 
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		 TbItemDesc tbItemDesc = descmapper.selectByPrimaryKey(itemId);
		 
		 //如果没缓存添加缓存，并设置过期时间
		    try {
				jedisClient.set(ITEM_INFO_KEY + ":" + itemId + ":DESC", JsonUtils.objectToJson(tbItemDesc));
				jedisClient.expire(ITEM_INFO_KEY + ":" + itemId + ":DESC",ITEM_INFO_KEY_EXPIRE);
			} catch (Exception e) {
				e.printStackTrace();
			}
		 
		 return tbItemDesc;
	}

}
